package com.solotiger.okex.service.impl;

import com.solotiger.okex.api.OkexApi;
import com.solotiger.okex.common.util.MD5Util;
import com.solotiger.okex.domain.FutureTicker;
import com.solotiger.okex.domain.FutureUserInfoResult;
import com.solotiger.okex.service.IOkexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import retrofit2.Call;
import retrofit2.Response;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Author: Lin.Qiang
 * Date:2018/7/30 0030
 */
@Service
public class OkexServiceImpl implements IOkexService {

    @Autowired
    private OkexApi okexApi;

    @Override
    public FutureTicker futureTicker(String symbol, String contract_type) {
        Call<FutureTicker> call = okexApi.futureTicker(symbol, contract_type);
        try {
            Response<FutureTicker> resultResponse = call.execute();
            return resultResponse.body();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public FutureUserInfoResult futureUserInfo(String apikey, String secretKey) {
        // 构造参数签名
        Map<String, String> params = new HashMap<String, String>();
        params.put("api_key", apikey);
        String sign = MD5Util.buildMysignV1(params, secretKey);
        params.put("sign", sign);

        Call<FutureUserInfoResult> call = okexApi.futureUserInfo(params);
        try {
            Response<FutureUserInfoResult> resultResponse = call.execute();
            return resultResponse.body();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
